package com.zywl.service.impl;

import com.zywl.core.beans.Company;
import com.zywl.core.beans.Member;
import com.zywl.core.dao.mapper.sys.CompanyMapper;
import com.zywl.core.dao.mapper.sys.MemberMapper;
import com.zywl.service.CompanyService;
import com.zywl.service.impl.base.BaseServiceImpl;
import com.zywl.utils.utils.GenerateCodeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl extends BaseServiceImpl<CompanyMapper, Company> implements CompanyService {
    @Autowired
    private MemberMapper memberMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Company t) throws Exception {
        mapper.insert(t);
        //添加协会的同时添加登入用户
        if(StringUtils.isNotEmpty(t.getComMobile())) {
            Map<String,Object> map = new HashMap<>();
            map.put("meb_username",t.getComMobile());
            map.put("meb_stt","0");
            List<Member> memberList = memberMapper.getListByParams(map);
            if(memberList.size() == 0){
                Member member = new Member();
                member.setMebType(t.getComType());
                member.setMebUsername(t.getComMobile());
                member.setMebPassword(GenerateCodeUtil.MD5("123456"));
                member.setMebNickname(t.getComLinkman());
                member.setMebAssociationId(t.getComAssociationId());
                member.setMebCompanyId(t.getComId());
                member.setMebMobile(t.getComMobile());
                member.setMebStt("0");
                member.setMebCreateTime(t.getComCreateTime());
                member.setMebModifyTime(t.getComCreateTime());
                memberMapper.insert(member);
            }else{
                Member member = memberList.get(0);
                member.setMebType(t.getComType());
                member.setMebNickname(t.getComLinkman());
                member.setMebCompanyId(t.getComId());
                member.setMebModifyTime(t.getComCreateTime());
                memberMapper.updateByPrimaryKey(member);
            }
        }
    }

    @Override
    public List<Company> getByMatch(Long companyId,List<String> industrysList , List<String> tagList)throws Exception{
        return mapper.getByMatch(companyId,industrysList,tagList);
    }

}
